/**
 * 
 */
package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * @author A.Yerzhanov
 * 07.12.2012
 */
public class Mhrmloanline extends X_hrm_loan_line {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4014800646845634461L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmloanline.class);

	/**
	 * @param ctx
	 */
	public Mhrmloanline(Properties ctx) {
		super(ctx);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ctx
	 * @param hrm_loan_line_ID
	 * @param trxName
	 */
	public Mhrmloanline(Properties ctx, int hrm_loan_line_ID, String trxName) {
		super(ctx, hrm_loan_line_ID, trxName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public Mhrmloanline(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// TODO Auto-generated constructor stub
	}
	
public static Mhrmloanline getLoanList(int C_BPartner_ID, Timestamp dt) {
	
	if (C_BPartner_ID == 0)
		return null;
	
	//
    PreparedStatement pstmt = null;
	ResultSet rs = null;
	Mhrmloanline result = null;
		
	// 
	String sql_ = "SELECT * FROM HRM_LOAN_LINE \n " +
					"WHERE ISACTIVE='Y' \n " +
						"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " + 
						"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
						"AND DATEFROM BETWEEN DATE_TRUNC('MONTH', " + DB.TO_DATE(dt, true) + ") " +
						"AND DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") + '1 MON'::INTERVAL - '1 DAY'::INTERVAL";		
		try
		{
			pstmt = DB.prepareStatement(sql_,null);				
			rs = pstmt.executeQuery();	
			if(rs.next())
				result = new Mhrmloanline(Env.getCtx(), rs, null);
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
	return result;	
	}

	/**
	 * Search for entries in the -->>> loanLine <<---
	 * 
	 * @param C_BPartner_ID
	 * @param Timestamp dt
	 * @return  Model loan line
	 */
	public static X_hrm_loanline getLoanLineAcct(int hrm_loan_line_ID, int reservesectional_ID) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		X_hrm_loanline result = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_LOANLINE WHERE HRM_LOAN_LINE_ID= " + hrm_loan_line_ID + " \n" +
				" AND HRM_RESERVESECTIONAL_ID=" + reservesectional_ID;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new X_hrm_loanline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	  }
}
